package com.volunteer.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.volunteer.common.R;
import com.volunteer.entity.Role;
import com.volunteer.service.RoleService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/admin/role")
@AllArgsConstructor
public class AdminRoleController {
    private final RoleService roleService;

    // 获取所有角色
    @GetMapping("/list")
    public R<List<Role>> list() {
        List<Role> list = roleService.list(new LambdaQueryWrapper<Role>().orderByAsc(Role::getId));
        return R.ok(list);
    }

    // 新增角色
    @PostMapping("/add")
    public R<String> add(@RequestBody Role role) {
        boolean success = roleService.save(role);
        return success ? R.ok("新增成功") : R.fail("新增失败");
    }

    // 更新角色
    @PostMapping("/update")
    public R<String> update(@RequestBody Role role) {
        boolean success = roleService.updateById(role);
        return success ? R.ok("更新成功") : R.fail("更新失败");
    }

    // 删除角色
    @DeleteMapping("/delete/{id}")
    public R<String> delete(@PathVariable Long id) {
        boolean success = roleService.removeById(id);
        return success ? R.ok("删除成功") : R.fail("删除失败");
    }
} 